package com.hemaapp.yjnh.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.CmnBookMerchantDetailActivity;
import com.hemaapp.yjnh.activity.CmnMerchantDetailActivity;
import com.hemaapp.yjnh.activity.FarmerDetailsActivity;
import com.hemaapp.yjnh.activity.MainActivity;
import com.hemaapp.yjnh.activity.MyFarmerActivity;
import com.hemaapp.yjnh.activity.WebviewActivity;
import com.hemaapp.yjnh.activity.ZouzouCampsiteMerchantDetailActivity;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.bean.YijiaRecommend;
import com.hemaapp.yjnh.utils.ImageUtils;

import xtom.frame.util.XtomWindowSize;

/**易家推荐的商品适配器
 * @author GuiyouZhang
 * Date：2015年12月22日 下午1:52:30
 * Email:guiyouzhang@163.com
 */
public class RecommendAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<YijiaRecommend> recommends = new ArrayList<YijiaRecommend>();
	private SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM月dd日");
	
	public RecommendAdapter(Context context, ArrayList<YijiaRecommend> recommends) {
		super(context);
		this.mContext = context;
		this.recommends = recommends;
	}
	
	@Override
	public int getCount() {
		int size = recommends == null ? 0 : recommends.size();
		return size == 0 ? 1 : recommends.size();
	}

	@Override
	public boolean isEmpty() {
		int size = recommends == null ? 0 : recommends.size();
		return size == 0;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (isEmpty()) 
			return getEmptyView(parent);
		ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_recommod, parent, false);
            holder = new ViewHolder();
            findViews(convertView, holder);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        }
		setData(position, convertView);
		return convertView;
	}

	private class ViewHolder {
		private ImageView ivBackground;
		private TextView tvName;
		private TextView tvDate;
	}
	
	private void findViews(View convertView, ViewHolder holder) {
		holder.ivBackground = (ImageView) convertView.findViewById( R.id.iv_background );
		holder.tvName = (TextView) convertView.findViewById( R.id.tv_name );
		holder.tvDate = (TextView) convertView.findViewById( R.id.tv_date );
	}

	private void setData(int position, View convertView) {
		final YijiaRecommend goods = recommends.get(position);
		ViewHolder holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        ImageUtils.loadBigImage(mContext, goods.getImgurl(), holder.ivBackground);
        holder.ivBackground.getLayoutParams().height = XtomWindowSize.getWidth(mContext) * 265 / 640;
		holder.tvName.setText(goods.getName());
		holder.tvDate.setText(getDate(goods.getRegdate()));
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String keyType = goods.getKeytype();// 广告类型	1：商品；2：商家；3：图文详情
				String keyId = goods.getKeyid();
				//保存广告点击记录
				String token = "";
				User user = BaseApplication.getInstance().getUser();
				if (user != null) {
					token = user.getToken();
				}
				((MainActivity)mContext).getNetWorker().ADRecord( token, "3", goods.getId());
				//执行相应操作
                Intent intent = new Intent();
				if (keyType.equals("1")) {
					intent = BaseUtil.createBlogIntent(mContext, goods.getType());
					intent.putExtra("blog_id", keyId);
					mContext.startActivity(intent);
				}else if (keyType.equals("2")) {
					String role = goods.getRole();
					//1田家用户;2地标商家;3村游商家;4省级代理;5运营商;6乐乐商家;
					//7特产商家;8粮库商家;11走走露营商家;12走走房车商家;
					//21苗木;22花卉;23家电;24日用商品
					if (role.equals("1")) {
						intent.setClass(mContext, FarmerDetailsActivity.class);
						if (user != null && keyId.equals(user.getId())) {
							intent.setClass(mContext, MyFarmerActivity.class);
						}
					}else if (role.equals("2") || role.equals("7")
							|| role.equals("8") || role.equals("12") || role.equals("21") 
							|| role.equals("23") || role.equals("24")|| role.equals("22")) {
						intent.setClass(mContext, CmnMerchantDetailActivity.class);
					}else if (role.equals("3") || role.equals("6") || role.equals("12")) {
						intent.setClass(mContext, CmnBookMerchantDetailActivity.class);
					}else if (role.equals("11")) {
						intent.setClass(mContext, ZouzouCampsiteMerchantDetailActivity.class);
					}
					intent.putExtra("client_id", keyId);
					mContext.startActivity(intent);
				} else if (keyType.equals("3")) {
					intent.setClass(mContext, WebviewActivity.class);
					String website = goods.getWebsite_url();
					if (isNull(website)) {
						intent.putExtra("parm", goods.getKeyid());
						intent.putExtra("type", 9);
					}else {
						intent.putExtra("parm", website);
						intent.putExtra("type", 12);
					}
					mContext.startActivity(intent);
				}
			}
		});
	}
	
	private String getDate(String date_buf) {
		try {
			Date date = dateFormat1.parse(date_buf);
			return dateFormat2.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
	}
}
